﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;

public partial class Main_ShopRegister : System.Web.UI.Page
{
    UsersDao ud = new UsersDao();
    UsersVo uv = new UsersVo();
    ShopDao sd = new ShopDao();
    PicDao pd = new PicDao();
    DataTable dt = null;
    private static int page = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            page = 0;
            dt = sd.getField();
            ddlfield.DataSource = dt;
            ddlfield.DataTextField = "Fname";
            ddlfield.DataValueField = "Fid";
            ddlfield.DataBind();
        }
    }

    protected void txtname_TextChanged(object sender, EventArgs e)
    {
        if (txtname.Text.Length < 4 || txtname.Text.Length > 16)
        {
            labcheck.Text = "<h3>*</h3>用户名须在<h3>4-16</h3>位，可以使用<h3>小写英文字母、数字以及中文</h3>组合。";
        }
        else
        {
            bool flag = CheckSpecialWord(txtname);
            if (flag)
            {
                labcheck.Text = "用户名不能含有特殊字符！";
            }
            else
            {
                string str = ud.CheckName(txtname.Text);
                if (str == "对不起，用户名已存在！")
                {
                    labcheck.Text = "对不起，用户名已存在！";
                }
                else
                {
                    labcheck.Text = "恭喜，用户名可用！";
                }
                Session.Add("pwd", txtpwd.Text);
                if (Session["pwd"] != null)
                {
                    txtpwd.Attributes.Add("value", Session["pwd"].ToString());
                    txtpwdagain.Attributes.Add("value", Session["pwd"].ToString());
                }
            }
        }
    }
    protected void txtshopname_TextChanged(object sender, EventArgs e)
    {
        bool flag = CheckSpecialWord(txtshopname);
        if (flag)
        {
            labshopcheck.Text = "店名不能含有特殊字符！";
        }
        else
        {
            string str = sd.CheckShopName(txtshopname.Text);
            if (str == "对不起，店名已存在！")
            {
                labshopcheck.Text = "对不起，店名已存在！";
            }
            else
            {
                labshopcheck.Text = "恭喜，店名可用！";
            }
        }
    }
    protected void ddlfield_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddltype.Items.Clear();
        ddltype.Items.Add("--请选择--");
        dt = sd.getType(ddlfield.SelectedValue);
        ddltype.DataSource = dt;
        ddltype.DataTextField = "Tname";
        ddltype.DataValueField = "Tid";
        ddltype.DataBind();
    }
    protected void btnuplogo_Click(object sender, EventArgs e)
    {
        string[] Extensions={ ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "logo", FileUplogo, Extensions, 50000,"Logo", imglogo);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnuptop1_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".jpeg" };
        string msg = UploadImg("shop", "pic", FileUptop1, Extensions, 100000, "top1", imgtop1);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnuptop2_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".jpeg" };
        string msg = UploadImg("shop", "pic", FileUptop2, Extensions, 100000, "top2", imgtop2);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnuptop3_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".jpeg" };
        string msg = UploadImg("shop", "pic", FileUptop3, Extensions, 100000, "top3", imgtop3);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnupinfo1_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo1, Extensions, 200000, "info1", imginfo1);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnupinfo2_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo2, Extensions, 200000, "info2", imginfo2);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnupinfo3_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo3, Extensions, 200000, "info3", imginfo3);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnupinfo4_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo4, Extensions, 200000, "info4", imginfo4);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnupinfo5_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo5, Extensions, 200000, "info5", imginfo5);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnupinfo6_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo6, Extensions, 200000, "info6", imginfo6);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnupinfo7_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo7, Extensions, 200000, "info7", imginfo7);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnupinfo8_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo8, Extensions, 200000, "info8", imginfo8);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }

    protected void btnuplicense_Click(object sender, EventArgs e)
    {
        string[] Extensions = {".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "license", FileUplicense, Extensions, 200000, "license", imglicense);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }
    protected void btnupidcard_Click(object sender, EventArgs e)
    {
        string[] Extensions = {".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "idcard", FileUpidcard, Extensions, 200000, "idcard", imgidcard);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnback_Click(sender, e);
        btnnext_Click(sender, e);
    }

    protected void btnback_Click(object sender, EventArgs e)
    {
        switch (page)
        {
            case 0:
                Panel2.Visible = true;
                Panel1.Visible = false;
                Panel3.Visible = false;
                Panel4.Visible = false;
                Panel5.Visible = false;
                btnback.Text = "上一步";
                btnnext.Text = "下一步";
                page += 2;
                break;
            case 1:
                Panel1.Visible = true;
                Panel2.Visible = false;
                Panel3.Visible = false;
                Panel4.Visible = false;
                Panel5.Visible = false;
                btnback.Text = "同意";
                btnnext.Text = "不同意";
                break;
            case 2:
                Panel2.Visible = true;
                Panel1.Visible = false;
                Panel3.Visible = false;
                Panel4.Visible = false;
                Panel5.Visible = false;
                break;
            case 3:
                Panel3.Visible = true;
                Panel1.Visible = false;
                Panel2.Visible = false;
                Panel4.Visible = false;
                Panel5.Visible = false;
                break;
            case 4:
                Panel4.Visible = true;
                Panel1.Visible = false;
                Panel2.Visible = false;
                Panel3.Visible = false;
                Panel5.Visible = false;
                break;
        }
        page--;
    }
    protected void btnnext_Click(object sender, EventArgs e)
    {
        switch (page)
        {
            case 0:
                Response.Redirect("../index.aspx");
                break;
            case 1:
                int count = 0;
                ArrayList list = new ArrayList();
                list.Add(" ");
                list.Add("(");
                list.Add(")");
                list.Add("+");
                list.Add("<");
                list.Add(">");
                list.Add(",");
                list.Add("/");
                list.Add("\\");
                list.Add(";");
                for (int i = 0; i < txtname.Text.Length; i++)
                {
                    if (list.Contains(txtname.Text[i].ToString()))
                    {
                        count = -1;
                    }
                }
                if (count < 0)
                {
                    string script = "alert('用户名不能含有特殊字符！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    return;
                }
                string str = ud.CheckName(txtname.Text);
                if (str == "对不起，用户名已存在！")
                {
                    string script = "alert('对不起，用户名已存在！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    return;
                }
                if (txtname.Text.Length < 4 || txtname.Text.Length > 16)
                {
                    string script = "alert('用户名须在4-16位！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    return;
                }
                if (txtpwd.Text == "" || txtname.Text == "" || txtpwdagain.Text == "" || txtemail.Text == "")
                {
                    string script = "alert('用户名密码和邮箱不能为空！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    return;
                }
                Panel3.Visible = true;
                Panel1.Visible = false;
                Panel2.Visible = false;
                Panel4.Visible = false;
                Panel5.Visible = false;
                hfpwd.Value = txtpwd.Text;
                break;
            case 2:
                if (ddlfield.SelectedValue == "0")
                {
                    string script = "alert('请选择所属领域！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    return;
                }
                if (ddltype.SelectedValue == "0")
                {
                    string script = "alert('请选择所属类型！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    return;
                }
                if (txttruename.Text == "")
                {
                    string script = "alert('请填写负责人姓名！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    return;
                }
                if (txtidcard.Text == "")
                {
                    string script = "alert('请填写身份证号码！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    return;
                }
                if (txtphone.Text == "")
                {
                    string script = "alert('请填写手机号码！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    return;
                }
                if (txtqq.Text == "")
                {
                    string script = "alert('请填写QQ号码！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    return;
                }
                Panel4.Visible = true;
                Panel1.Visible = false;
                Panel2.Visible = false;
                Panel3.Visible = false;
                Panel5.Visible = false;
                break;
            case 3:
                Panel5.Visible = true;
                Panel1.Visible = false;
                Panel2.Visible = false;
                Panel3.Visible = false;
                Panel4.Visible = false;
                btnnext.Text = "完成";
                break;
            case 4:
                uv.Uname = txtname.Text;
                uv.Upwd = hfpwd.Value;
                uv.Uemail = txtemail.Text;
                uv.Utruename = txttruename.Text;
                uv.Uidcard = txtidcard.Text;
                uv.Uphone = txtphone.Text;
                uv.Uqq = txtqq.Text;
                Exception ex = new Exception();
                ex = ud.addShopUsers(uv);
                if (ex == null)
                {
                    SetUserFolder(txtname.Text);
                    Session.Remove("pwd");
                    uv = ud.UsersLogin(txtname.Text, hfpwd.Value);
                    Session.Add("user", uv);
                    ShopVo sv = new ShopVo();
                    sv.Sname = txtshopname.Text;
                    sv.Fid = Convert.ToInt32(ddlfield.SelectedValue);
                    sv.Tid = Convert.ToInt32(ddltype.SelectedValue);
                    sv.Sintroduce = txtintrduce.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
                    sv.Scontent = txtcontent.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
                    sv.Sjob = txtjob.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
                    sv.Saddress = txtaddress.Text;
                    sv.Stel = txttel.Text;
                    sv.Slogo = imglogo.ImageUrl;
                    sv.Spic = imgtop1.ImageUrl + "|" + imgtop2.ImageUrl + "|" + imgtop3.ImageUrl;
                    sv.Shot = 1;
                    sv.Sstate = 1;
                    sv.Uid=uv.Uid;
                    sv.Slicense=imglicense.ImageUrl;
                    sv.Sidcard=imgidcard.ImageUrl;
                    ex = sd.addShop(sv);
                    if (ex == null)
                    {
                        int sid = sd.getNewShopId(txtshopname.Text);
                        //添加8张图片的信息
                        PicVo pv = new PicVo();
                        pv.Id = sid;
                        pv.Ptype = 0;
                        if (imginfo1.ImageUrl!="")
                        {
                            pv.Purl = imginfo1.ImageUrl;
                            pv.Pname = txtdesinfo1.Text;
                            pd.addPic(pv);
                        }
                        if (imginfo2.ImageUrl != "")
                        {
                            pv.Purl = imginfo2.ImageUrl;
                            pv.Pname = txtdesinfo2.Text;
                            pd.addPic(pv);
                        }
                        if (imginfo3.ImageUrl != "")
                        {
                            pv.Purl = imginfo3.ImageUrl;
                            pv.Pname = txtdesinfo3.Text;
                            pd.addPic(pv);
                        }
                        if (imginfo4.ImageUrl != "")
                        {
                            pv.Purl = imginfo4.ImageUrl;
                            pv.Pname = txtdesinfo4.Text;
                            pd.addPic(pv);
                        }
                        if (imginfo5.ImageUrl != "")
                        {
                            pv.Purl = imginfo5.ImageUrl;
                            pv.Pname = txtdesinfo5.Text;
                            pd.addPic(pv);
                        }
                        if (imginfo6.ImageUrl != "")
                        {
                            pv.Purl = imginfo6.ImageUrl;
                            pv.Pname = txtdesinfo6.Text;
                            pd.addPic(pv);
                        }
                        if (imginfo7.ImageUrl != "")
                        {
                            pv.Purl = imginfo7.ImageUrl;
                            pv.Pname = txtdesinfo7.Text;
                            pd.addPic(pv);
                        }
                        if (imginfo8.ImageUrl != "")
                        {
                            pv.Purl = imginfo8.ImageUrl;
                            pv.Pname = txtdesinfo8.Text;
                            pd.addPic(pv);
                        }
                        string script = "alert('注册成功！');location.href='../index.aspx';";
                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    }
                    else
                    {
                        string script = "alert('注册失败！');";
                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                    }
                }
                else
                {
                    string script = "alert('注册失败！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                }
                break;
        }
        page++;
    }

    public bool SetUserFolder(string username)
    {
        bool result = true;
        string SpacePath = HttpContext.Current.Server.MapPath("../images/users/" + username);
        try
        {
            if (!Directory.Exists(SpacePath))
            {
                if (Directory.CreateDirectory(SpacePath) == null) result = false;
            }
        }
        catch
        {
            return false;
        }
        return result;
    }

    public string UploadImg(string SorH, string type, FileUpload fu, string[] Extensions,int size,string info,Image img)
    {
        bool fileOK = false;
        string path = Server.MapPath("../images/" + SorH + "/" + type + "/") + fu.FileName;
        if (fu.FileName != "")
        {
            if (fu.HasFile)
            {
                string fileExtension = Path.GetExtension(fu.FileName).ToLower();
                string[] allowerExtensions = Extensions;
                for (int i = 0; i < allowerExtensions.Length; i++)
                {
                    if (fileExtension == allowerExtensions[i])
                    {
                        fileOK = true;
                    }
                }
                if (!fileOK)
                {
                    return "图片格式不正确！";
                }
                else
                {
                    if (fu.PostedFile.ContentLength <= size)
                    {
                        string picPath = path.Replace(fu.FileName, txtshopname.Text + info + fileExtension);
                        fu.PostedFile.SaveAs(picPath);
                        bool flag = false;
                        //最后一部高级验证，图片上传后的操作，判断是否真的是图片
                        StreamReader sr = new StreamReader(picPath, Encoding.Default);
                        string strContent = sr.ReadToEnd();
                        sr.Close();
                        string str = "request|script|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=";
                        foreach (string s in str.Split('|'))
                        if (strContent.IndexOf(s) != -1)
                        {
                            File.Delete(picPath);
                            flag = true;
                            break;
                        }
                        if (!flag)
                        {
                            img.ImageUrl = "../images/" + SorH + "/" + type + "/" + txtshopname.Text + info + fileExtension;
                            img.Visible = true;
                            return "上传图片成功！";
                        }
                        else
                        {
                            return "图片格式非法！";
                        }
                    }
                    else
                    {
                        return "图片大小不能超过" + (size / 1000).ToString() + "K！";
                    }
                }
            }
            else
            {
                return "上传失败，格式不允许！";
            }
        }
        else
        {
            return "上传不能为空！";
        }
    }

    public bool CheckSpecialWord(TextBox txtbox)
    {
        bool flag = false;
        ArrayList list = new ArrayList();
        list.Add(" ");
        list.Add("~");
        list.Add("!");
        list.Add("@");
        list.Add("#");
        list.Add("$");
        list.Add("%");
        list.Add("^");
        list.Add("&");
        list.Add("*");
        list.Add("(");
        list.Add(")");
        list.Add("_");
        list.Add("+");
        list.Add("<");
        list.Add(">");
        list.Add(",");
        list.Add(".");
        list.Add("/");
        list.Add("\\");
        list.Add("?");
        list.Add(";");
        for (int i = 0; i < txtbox.Text.Length; i++)
        {
            if (list.Contains(txtbox.Text[i].ToString()))
            {
                flag = true;
            }
        }
        return flag;
    }
}
